// pages/contract/contract.js
const wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    justImg2: '../../images/s4.png',
    backImg2: '../../images/s5.png',
    dataImg: '../../images/s7.png',
    name:'',
    currentTab: 0,
    credit_card_positive_photo_2:'',
    credit_card_negative_photo_2:'',
    car_certificate_photo:'',
    lid:'',
    loan_id:''
  },
  bindName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  formSubmit:function(e){
    if (this.data.name==''){
      wx.showToast({
        title: '请输入开票单位全称',
        icon: 'none',
      }); 
    }else{
      let postUrl = '/api/madetoorder.loan.index/stage_two_apply',
        that = this,
      params={
        lu_id:that.data.lid,
        credit_card_positive_photo_2: that.data.credit_card_positive_photo_2,
        credit_card_negative_photo_2: that.data.credit_card_negative_photo_2,
        car_certificate_photo: that.data.car_certificate_photo,
        invoice_company_name: that.data.name
      };
      wh.Post(postUrl, params, function (res) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          complete: function () {
            setTimeout(function () {
              wx.switchTab({
                url: '../person/person',
              })
            }, 2000);
          }
        }); 
      })
    }
  },
  // 银行卡正面
  uploadPic4: function (e) {
    var that = this,
    param_name = e.currentTarget.dataset.param;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let filePath = res.tempFilePaths[0],
          postUrl = '/api/madetoorder.loan.index/add_user_material';
        let data = wh.Wxupload(postUrl, filePath, param_name).then(function (res) {
          switch (param_name) {
            case 'credit_card_positive_photo_2':
              that.setData({
                justImg2: filePath,
                credit_card_positive_photo_2: res.path
              });
              break;
            case 'credit_card_negative_photo_2':
              that.setData({
               backImg2: filePath,
                credit_card_negative_photo_2: res.path
              });
              break;
            case 'car_certificate_photo':
              that.setData({
                dataImg: filePath,
                car_certificate_photo: res.path
              });
              break;
          }
        });
      }
    })
  },
  bindName: function (e) {
    this.setData({
      name:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      lid:options.lu_id,
      loan_id: options.loan_id
    })
    this.data.currentTab = options.currentTab;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      currentTab_back: this.data.currentTab
    });
  }
})